package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.service.SmsService;
import com.itheima.health.util.ValidateCodeUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 验证码Controller
 */
@RestController
@RequestMapping("/mobile/validateCode")
@Slf4j
@Api(tags = "手机端验证码接口")
public class MobileValidateCodeController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SmsService smsService;


    @PostMapping("/send")
    public Result sendMsg(String type, String telephone) {
//        log.info("请求的验证码类型是:"+type);
//        String code = (String) redisTemplate.opsForValue().get(telephone+"MOBILE_SIGNIN");
//        if (code == null) {
//            //当前redis中此telephone 为null 则生成验证码(String)
//            String msgCode = String.valueOf(ValidateCodeUtils.generateValidateCode(6));
//            //在redis中新增一个字符串类型的值，并且设置变量值的过期时间
//            log.info("当前登陆的手机号为:"+telephone + ",验证码为:"+msgCode);
//            redisTemplate.opsForValue().set(telephone+type,msgCode,2, TimeUnit.MINUTES);
//                //短信验证服务
//                //GuoYangYun.sendMsg(telephone,msgCode);
//        }else {
//            log.info("当前登陆的手机号为:"+telephone + ",验证码为:"+code);
//            throw new BusinessRuntimeException("验证码已经发送！请2分钟后在重新发送");
//        }
//        return new Result(true,MessageConst.SEND_VALIDATECODE_SUCCESS);
//    }
        log.info("[验证码-发送]type:{},telephone:{}", type, telephone);
        //rpc调用发送验证码
        smsService.sendValidateCode(type, telephone);
        return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);

    }
}